package com.hgm.flopgame2.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.hgm.flopgame2.R
import com.hgm.flopgame2.database.Score

class ScoreAdapter(var scoreList: List<Score>,var user:String) : RecyclerView.Adapter<ScoreAdapter.MyViewHolder>() {


    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val bg: RelativeLayout=view.findViewById(R.id.background)
        val head: LinearLayout = view.findViewById(R.id.item_head)
        val num: TextView = view.findViewById(R.id.item_num)
        val name: TextView = view.findViewById(R.id.item_name)
        val time: TextView = view.findViewById(R.id.item_time)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        var view = LayoutInflater.from(parent.context).inflate(R.layout.item_score, null)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        var score = scoreList[position]


        holder.apply {
            num.text = "${position + 1}"
            name.text = "${score.name}"

            //时间转换
            var timeCount = score.time//总时长
            var min = timeCount / 60//分
            var sec = timeCount % 60//秒

            var value1 = if (min < 10) "0$min" else min//分
            var value2 = if (sec < 10) "0$sec" else sec//秒
            time.text = "$value1:$value2"

            //前三名头像
            when (position) {
                0 -> holder.head.setBackgroundResource(R.drawable.item1)
                1 -> holder.head.setBackgroundResource(R.drawable.item2)
                2 -> holder.head.setBackgroundResource(R.drawable.item3)
            }

            //高亮显示
            if (name.text==user) {
                bg.setBackgroundResource(R.drawable.shape_score_item)
                num.setTextColor(Color.RED)
                name.setTextColor(Color.RED)
            }

        }
    }

    override fun getItemCount(): Int {
        return scoreList.size
    }
}
